package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "套餐相关接口")
@RequestMapping("/admin/setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key="#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {

        log.info("新增套餐");
        setMealService.save(setmealDTO);

        return Result.success();

    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation(value = "套餐分页查询")
    @GetMapping("/page")
//    @CachePut(cacheNames = "setmealCache",key="#setmealPageQueryDTO.categoryId")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO)
    {
          log.info("套餐分页查询开始******************");
           PageResult result = new PageResult();
           Page page =setMealService.page(setmealPageQueryDTO);
           result.setTotal(page.getTotal());
           result.setRecords(page.getResult());
        log.info("分页查询结束**************");
        return Result.success(result);


    }

    /**
     * 起售停售套餐
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "起售停售套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)//这个地方虽然只涉及到一个分类下的套餐变化，但是没有分类id，直接偷懒了，把全部分类的套餐数据缓存删除
    public Result startOrStop(@PathVariable Integer status, Long id) {

        log.info("起售停售套餐");
        setMealService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询套餐")
//    @CachePut (cacheNames = "setmealCache",key="#id")//如果移动端根本就不访问这个功能（同一个功能两个url，一个是管理端一个是用户端）那我把结果更新在缓存干啥
    public Result<SetmealVO> getById(@PathVariable Long id)
    {
        log.info("根据id查询套餐{}",id);
       SetmealVO setmealVO = setMealService.getById(id);
        return Result.success(setmealVO);
    }
    @DeleteMapping
    @ApiOperation(value = "批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids)
    {
        log.info("批量删除套餐{}",ids);
        setMealService.deleteBatch(ids);
        return Result.success();
    }
    @PutMapping
    @ApiOperation(value = "修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO)
    {
        log.info("修改套餐{}",setmealDTO);
        setMealService.update(setmealDTO);
        return Result.success();

    }

}
